package com.capgemini.diabeo.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.provider.MediaStore.Images;
import android.text.Html;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.capgemini.diabeo.MealChoice;
import com.capgemini.diabeo.R;
import com.capgemini.diabeo.R.menu;
import com.capgemini.diabeo.adapter.DishListAdapter;
import com.capgemini.diabeo.adapter.DishListAdapter_;
import com.capgemini.diabeo.carousel.MainCarousel_;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.MealStatus;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.model.Menu;
import com.capgemini.diabeo.model.MenuDish;
import com.capgemini.diabeo.nav.SampleListActivity_;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ItemLongClick;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_menu)
@OptionsMenu(R.menu.menu)
public class MenuActivity extends Activity {

	public static final String PREFS_GENERAL = "PrefsGenerales";

	@ViewById
	TextView dishCount;

	@ViewById
	TextView carbohydrateCount;

	@ViewById
	TextView lipidCount;

	@ViewById
	TextView iGCount;

	@Bean
	DishListAdapter dishAdapter;

	@ViewById
	ListView dishesList;

	@Bean
	DishManager dishManager;

	@AfterViews
	public void afterViews() {
		dishesList.setAdapter(dishAdapter);
		String time ="";
		Date creationDate = dishManager.getCurrentMenu().getCreationDate();
		//TOMODIFY
		CharSequence date = DateFormat.format("EEEE dd MMMM yyyy", creationDate);
		String currentTime = dishManager.getCurrentMenu().getMealTime();
		String menuMatin = MealStatus.BREAKFAST_MENU.getValue();
		String menuMidi = MealStatus.LUNCH_TIME_MENU.getValue();
		String menuSoir = MealStatus.DINNER_MENU.getValue();
		if(currentTime.equals(menuMatin))
		{
			time = "Matin";
		}
		else if (currentTime.equals(menuMidi)) {
			time = "Midi";
			
		}else if (currentTime.equals(menuSoir)) {
			time = "Soir";
		}
		setTitle("Menu " + date + " "+ time);
	}



	@Override
	protected void onResume() {
		super.onResume();
		List<Dish> dishes = dishManager.getMenuDishes();
		dishAdapter.setDishList(dishes);
		dishAdapter.removeAddButton();
		updateView();
	}

	private void updateView() {

		Menu menu = dishManager.getCurrentMenu();
		carbohydrateCount.setText(String.valueOf(menu.getTotalCarbohydrate()));
		iGCount.setText(String.valueOf(menu.getTotalIG()));
		lipidCount.setText(String.valueOf(menu.getTotalLipid()));
		dishCount.setText(String.valueOf(menu.getMenuDishList().size()));

		/**Calcul : On fait par rapport au calcul (glucide x rapport) / 10 **/
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		TextView doseCount = (TextView)findViewById(R.id.doseCount);
		String rapportRepas = prefs.getString(menu.getMealTime(), "1");
		Float rapportRepasMatinToFloat = Float.parseFloat(rapportRepas);
		Float totalGlucides = (float) menu.getTotalCarbohydrate();
		Float dose = (totalGlucides * rapportRepasMatinToFloat)/10;
		if(dose>0)
		{
			doseCount.setText("Dose d'insuline : "+dose);
		}
	}

	@Click
	public void addDishClicked() {
		MainCarousel_.intent(this).start();
	}

	@ItemLongClick
	public void dishesListItemLongClicked(Dish dish) {
		removeDish(dish);

	}

	@UiThread
	public void removeDish(Dish dish) {
		dishManager.removeDishToMenu(dish);
		dishAdapter.setDishList(dishManager.getMenuDishes());
		dishAdapter.notifyDataSetChanged();
		updateView();
	}

	@Click
	public void newMenuClicked(View view) {
		dishManager.createNewMenu();
		dishesList.setAdapter(DishListAdapter_.getInstance_(this));
		updateView();

	}

	@Click
	public void graphicClicked(View view) {
		GraphicActivity_.intent(this).start();
	}

	@OptionsItem(R.id.action_email)
	void shareEmail() {

		Menu menu = dishManager.getCurrentMenu();
		ArrayList<Uri> uris = new ArrayList<Uri>();
		StringBuilder c = new StringBuilder();
		Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
		intent.setType("*/*");
		intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Mon repas actuel");

		for (MenuDish dish : menu.getMenuDishList()) {
			String dishName = "<font color='red'>Nom du plat : </font>" + dish.getDish().getName();
			String infosGlucides = "Glucides : " + dish.getDish().getCarbohydrate() + " IG : " + dish.getDish().getIg() + " Lipides : " + dish.getDish().getLipid();
			byte[] imageBytes = dish.getDish().getImage();
			Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
			String url = Images.Media.insertImage(this.getContentResolver(), imageBitmap, "dish", null);
			Uri uri = Uri.parse(url);	
			c.append(dishName);
			c.append(" <br /> <font color='blue'>Informations glycémiques : </font>" + infosGlucides);
			uris.add(uri);
			c.append("<hr />");
			c.append("<br />");
		}
		intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(c.toString()));
		intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
		startActivity(Intent.createChooser(intent, "Share with"));

	}

}
